package com.lsmo.android.carfans.activity.car;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lsmo.android.carfans.R;
import com.lsmo.android.carfans.app.widget.RemoteImageView;
import com.lsmo.android.carfans.model.car.Brand;
import com.lsmo.android.util.ArrayListAdapter;

public class BrandListAdapter extends ArrayListAdapter<Brand>{

	public final static int TYPE_SECTION_HEADER = 0;
	
	public BrandListAdapter(Context context) {
		super(context);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View localView1 = convertView;
	    ViewHolder localViewHolder = new ViewHolder();
	    LayoutInflater localLayoutInflater = ((Activity) mContext).getLayoutInflater();
	    InputStream localInputStream;
	    //获取list数据
		//Map mapinfo = (Map) mList.get(position);
	    if(localView1 == null ){
		    	localView1 = localLayoutInflater.inflate(R.layout.car_adapter_brandlist, null);
		    	localViewHolder.name = (TextView)localView1.findViewById(R.id.brandname);
		    	localViewHolder.price = (TextView)localView1.findViewById(R.id.brandprice);
		    	localViewHolder.imageView = (RemoteImageView)localView1.findViewById(R.id.carbrandimage);
		    	
		    	localView1.setTag(localViewHolder);
	    } else {
	    	localViewHolder = (ViewHolder) localView1.getTag();
	    }
		try {
		    //图标
	    	AssetManager localAssetManager = this.mContext.getResources().getAssets();
	    	String str1 = ((Brand)mList.get(position)).getImage();
//	    	String str1 = (String) mapinfo.get("Image");
			localInputStream = localAssetManager.open(str1);
			
			RemoteImageView localRemoteImageView2 = localViewHolder.imageView;
		    String str2 = ((Brand)mList.get(position)).getImage();
		    Drawable localDrawable = Drawable.createFromStream(localInputStream, str2);
		    localRemoteImageView2.setBackgroundDrawable(localDrawable);
		    
		    //汽车名称name
		    String str3 =  ((Brand)mList.get(position)).getName();
		    localViewHolder.name.setText(str3);
		    
		    //汽车价格范围
		    String str4 = ((Brand)mList.get(position)).getMinValue();
		    if(!"".equals(str4) && str4 !=null){
		    	StringBuilder localStringBuilder1 = new StringBuilder("经销商报价: ");
		    	String str5 = ((Brand)mList.get(position)).getMinValue();
		    	localStringBuilder1.append(str5).append("万 ~ ");
		    	String str6 =((Brand)mList.get(position)).getMaxValue();
		    	localViewHolder.price.setText(localStringBuilder1.toString()+ str6 + "万");
		    } else {
		    	localViewHolder.price.setText("经销商报价: 暂无");
		        Integer localInteger = Integer.valueOf(R.drawable.default_image);
		        localViewHolder.imageView.setDefaultImage(localInteger);
//		        String strImg = ((Brand)mList.get(position)).getImage();
//		        ListView localListView = getListView();
//		        localViewHolder.imageView.setImageUrl(strImg, position, localListView);
		    }
		    
//		    String str8 = ((Brand)mList.get(position)).getEName();
//		    localViewHolder.name.setText(str8);
//		    localViewHolder.price.setText("未上市");
//		    localViewHolder.imageView.setBackgroundResource(R.drawable.default_image);
		    
//		    localView1 = localLayoutInflater.inflate(R.layout.adapter_brand_title, null);
//		    TextView localTextView6 = (TextView)localView1.findViewById(R.id.brandname);
//		    String str9 = ((Brand)mList.get(position)).getName();
//		    localTextView6.setText(str9);
		    
		} catch (IOException e) {
			e.printStackTrace();
		}
	    
		return localView1;
	}
	
	public boolean isEnabled(int position) {
		return (getItemViewType(position) != TYPE_SECTION_HEADER);
	}
	
	static class ViewHolder
	{
	    RemoteImageView imageView;
	    TextView name;
	    TextView price;
	}

}
